var httpConf = require("../../../conf/httpModule.js")
var conf = require("../../../conf/niuniuConf.js").niuConf
var utils = require("../../../util/util.js")
var async = require("async")
module.exports = function(app) {
	return new DBRemote(app);
};

var DBRemote = function(app) {
	this.app = app
	DBRemote.app = app
    DBRemote.dbService = this.app.get("dbService")
    if(DBRemote.dbService && DBRemote.dbService.db){
    	DBRemote.db = DBRemote.dbService.db
    }
}


//判断playerid玩家是否可加入uid玩家开的房间  返回true代表可加入，false代表不可加入
DBRemote.prototype.checkClubLimit = function(uid,playerId,cb) {
	if(uid == playerId){
		cb(true)
		return
	}
	var cmd = "nn:acc:"+uid+":"+"clubList"
	//查询是否开启仅俱乐部成员可加入
	DBRemote.dbService.db.hget(cmd,"switch",function(err,value) {
		if(err || value == "false"){
			cb(true)
			return
		}
		//查询成员是否在列表中
		DBRemote.dbService.db.hexists(cmd,playerId,function(err,value) {
			if(value == true){
				cb(true)
			}else{
				cb(false)
			}
		})
	})
}

//改变俱乐部开启开关
DBRemote.prototype.clubSwitch = function(uid,flag,cb) {
	var cmd = "nn:acc:"+uid+":"+"clubList"
	DBRemote.dbService.db.hset(cmd,"switch",flag,function(err,value) {
		if(!err){
			cb(true)
		}
	})
}
//获取俱乐部成员列表
DBRemote.prototype.getClubList = function(uid,cb) {
    var cmd = "nn:acc:"+uid+":"+"clubList"
    //俱乐部成员列表
	DBRemote.dbService.db.hgetall(cmd,function(err,data){
		if(err){
			cb(false)
		}else{
			cb(data)
		}
	})
}
//添加俱乐部成员
DBRemote.prototype.addClubPlayer = function(uid,playerId,cb) {
	 DBRemote.dbService.getPlayerInfoByUid(playerId,function(data) {
	 	if(data && playerId == data.uid){
		 	var tmpInfo = {
		 		"uid" : data.uid,
		 		"head" : data.head,
		 		"nickname" : data.nickname
		 	}
	 		var cmd = "nn:acc:"+uid+":"+"clubList"
 			DBRemote.dbService.db.hset(cmd,playerId,JSON.stringify(tmpInfo),function(err,value) {
 				if(value == 1){
 					cb(tmpInfo)
 				}else{
 					cb(false)
 				}
 			})
	 	}else{
	 		cb(false)
	 	}
	 })
}
//删除俱乐部成员
DBRemote.prototype.removePlayer = function(uid,playerId,cb) {
	var cmd = "nn:acc:"+uid+":"+"clubList"
	DBRemote.dbService.db.hdel(cmd,playerId,function(err,value) {
		if(value == 1){
			cb(true)
		}else{
			cb(false)
		}
	})
}
//申请加入俱乐部
DBRemote.prototype.applyJoinClub = function(uid,teaMasterUid,cb) {

	async.waterfall([
		function(next) {
			//判断不在俱乐部中
		    var cmd = "nn:acc:"+teaMasterUid+":"+"clubList"
		    //俱乐部成员列表
			DBRemote.dbService.db.hgetall(cmd,function(err,data){
				if(err){
					cb(false)
				}else{
					if(data && data[uid]){
						cb(false)
					}else{
						next()
					}
				}
			})
		},
		function(next) {
			//判断不在申请列表中
		    var cmd = "nn:acc:"+teaMasterUid+":"+"clubApplyList"
		    //俱乐部成员列表
			DBRemote.dbService.db.hgetall(cmd,function(err,data){
				if(err){
					cb(false)
				}else{
					if(data && data[uid]){
						cb(false)
					}else{
						next()
					}
				}
			})
		},
		function() {
			//加入申请列表
			DBRemote.dbService.getPlayerInfoByUid(uid,function(data) {
				if(data && uid == data.uid){
				 	var tmpInfo = {
				 		"uid" : data.uid,
				 		"head" : data.head,
				 		"nickname" : data.nickname
				 	}
					var cmd = "nn:acc:"+teaMasterUid+":"+"clubApplyList"
			 		DBRemote.dbService.db.hset(cmd,uid,JSON.stringify(tmpInfo),function(err,value) {
			 			if(value == 1){
			 				cb(true)
			 			}else{
			 				cb(false)
			 			}
			 		})
				}else{
					cb(false)
				}
			})
		}
		],function(err) {
		console.log(err)
	})

}
//获取申请列表
DBRemote.prototype.getApplyList = function(uid,cb) {
    var cmd = "nn:acc:"+uid+":"+"clubApplyList"
    //俱乐部成员列表
	DBRemote.dbService.db.hgetall(cmd,function(err,data){
		if(err){
			cb(false)
		}else{
			cb(data)
		}
	})
}
//同意申请
DBRemote.prototype.agreeApply = function(uid,playerId,cb) {
	var cmd = "nn:acc:"+uid+":"+"clubApplyList"
	DBRemote.dbService.db.hget(cmd,playerId,function(err,data){
		if(err || !data){
			cb(false)
		}else{
	 		cmd = "nn:acc:"+uid+":"+"clubList"
 			DBRemote.dbService.db.hset(cmd,playerId,data,function(err,value) {
 				cmd = "nn:acc:"+uid+":"+"clubApplyList"
	 			DBRemote.dbService.db.hdel(cmd,playerId,function(err,value) {
	 				cb(true)
	 			})
 			})
 			var str = "你的茶楼申请已被同意"
 			DBRemote.app.rpc.game.remote.sendTipsByUid(null,playerId,str,function(){})
		}
	})
}
//拒绝申请
DBRemote.prototype.rejectApply = function(uid,playerId,cb) {
	var cmd = "nn:acc:"+uid+":"+"clubApplyList"
	DBRemote.dbService.db.hget(cmd,playerId,function(err,data){
		if(err || !data){
			cb(false)
		}else{
	 		var cmd = "nn:acc:"+uid+":"+"clubApplyList"
 			DBRemote.dbService.db.hdel(cmd,playerId,function(err,value) {
 				cb(true)
 			})
  			var str = "你的茶楼申请已被拒绝"
 			DBRemote.app.rpc.game.remote.sendTipsByUid(null,playerId,str,function(){})
		}
	})
}